package com.ca.ui.core.view.button;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;

public class RoundButton extends ConstraintLayout {

    private String text = "";
    private boolean enable = true;
    private int type = 0;

    public TextView tv;

    public RoundButton(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public RoundButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public RoundButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.round_button_layout, this);
        tv = view.findViewById(R.id.tv);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundButton, 0, 0);

            text = array.getString(R.styleable.RoundButton_rb_text);
            enable = array.getBoolean(R.styleable.RoundButton_rb_enable, true);
            type = array.getInteger(R.styleable.RoundButton_rb_type, 0);

            array.recycle();
        }

        setText(text);
        setEnable(enable);
        setType(type);

        setOnClickListener( v -> {});
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text == null ? "" : text;
        this.tv.setText(this.text);
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
        super.setEnabled(this.enable);
        if (!this.enable) {
            setType(1);
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;

        int color;
        if (1 == type) {
            color = getResources().getColor(R.color.disabled_text);
            setAlpha(0.2f);
        } else {
            color = getResources().getColor(R.color.primary);
            setAlpha(0.6f);
        }
        tv.setTextColor(color);
    }
}
